package labuladong.leetcode.editor.cn._01linkedlist.ch00;

import labuladong.leetcode.editor.cn._00bean.ListNode;

import static labuladong.leetcode.editor.cn._00utils.LinkedListUtils.build;
import static labuladong.leetcode.editor.cn._00utils.LinkedListUtils.print;

public class _19_RemoveNthNodeFromEndOfList {

    //leetcode submit region begin(Prohibit modification and deletion)
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     * int val;
     * ListNode next;
     * ListNode() {}
     * ListNode(int val) { this.val = val; }
     * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */
    class Solution {
        // 主函数
        public ListNode removeNthFromEnd(ListNode head, int n) {
            // 虚拟头结点
            ListNode dummy = new ListNode(-1);
            dummy.next = head;
            // 删除倒数第 n 个，要先找倒数第 n + 1 个节点
            ListNode x = findFromEnd(dummy, n + 1);
            // 删掉倒数第 n 个节点
            x.next = x.next.next;
            return dummy.next;
        }

        // 返回链表的倒数第 k 个节点
        ListNode findFromEnd(ListNode head, int k) {
            ListNode p1 = head;
            // p1 先走 k 步
            for (int i = 0; i < k; i++) {
                p1 = p1.next;
            }
            ListNode p2 = head;
            // p1 和 p2 同时走 n - k 步
            while (p1 != null) {
                p2 = p2.next;
                p1 = p1.next;
            }
            // p2 现在指向第 n - k 个节点
            return p2;
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)

    public static void main(String[] args) {
        Solution solution = new _19_RemoveNthNodeFromEndOfList().new Solution();

        int[] nums1 = {1, 2, 3, 4, 5};
        ListNode link1 = build(nums1);
        print(link1);

        ListNode res = solution.removeNthFromEnd(link1, 3);
        print(res);
    }
}
